<template>
  <div class="register">
    <x-header v-show="!isWeChar">
      <div slot="default" class="common__header--title">{{pageTitle}}</div>
      <div @click="goBack" slot="overwrite-left"><img slot="icon" src="~assets/x3/ico/goback1.png" class="go-back"></div>
    </x-header>
    <!-- 个人用户注册 -->
    <div class="register_inputDiv">
      <x-input type="text" v-model="employeeCode" :min="StrLenth.eCodeMin" :max="StrLenth.eCodeMax" placeholder="请输入员工编码">
        <img slot="label" style="padding-right:10px;width: 19px;margin-left: -3px;display:block;" src="~assets/x3/channel/register/(0).png" width="18">
      </x-input>
      <x-input type="text" v-model="nickName" :min="StrLenth.nickNameMin" :max="StrLenth.nickNameMax" placeholder="请输入用户名">
        <img slot="label" style="padding-right:10px;width: 19px;margin-left: -3px;display:block;" src="~assets/x3/ico/ico-input-header.png" width="18">
      </x-input>
      <x-input type="tel" mask="99999999999" v-model="mobile" :max="StrLenth.mobile" placeholder="请输入手机号码" :disabled="isSendOk">
        <img slot="label" style="padding-right:10px;display:block;" src="~assets/x3/ico/ico-input-linkMobile.png" width="18">
      </x-input>
      <x-input v-if="haveImgCode" v-model="imgCode" :max="StrLenth.img" placeholder="请输入图形码">
        <img slot="label" style="padding-right:10px;padding-top:5px;display:block;" src="~assets/x3/ico/ico-input-img_code.png" width="18">
        <span slot="right" class="img__code--change" @click="changeImgCode()">换一张</span>
        <img slot="right" class="img__code" :src="imgCodeSrc" @click="changeImgCode()">
      </x-input>
      <x-input type="tel" :max="StrLenth.sms" v-model="mobileCode" placeholder="请输入短信验证码">
        <img slot="label" style="padding-right:10px;padding-top:5px;display:block;" src="~assets/x3/ico/ico-input-Mcode.png" width="18">

        <x-button action-type="button" slot="right" mini class="linkMobileCode" @click.native="sendSmsCode" :text="sendBtnText" :disabled="isSendOk"></x-button>
      </x-input>
    </div>
    <!-- 协议勾选 -->
    <div class="row_protocol">
      <span @click="confirmItem">
        <img v-show="isCheck" class="insure__img" src="~assets/x3/ico/check.png">
        <img v-show="!isCheck" class="insure__img" src="~assets/x3/ico/check_un.png">
      </span>
      <span class="insure__sure">
        <span @click="confirmItem">已阅读并同意</span>
        <a href="JavaScript:void(0);" @click="clauseRead('registerUserProtocol','恒华出行宝注册用户协议')">《用户协议》</a>
      </span>
    </div>

    <!-- 显示协议的弹窗 -->
    <x-dialog v-model="showDialog" class="dialog-Einv" hide-on-blur>
      <div style="padding:10px">
        <div class="protocol_content" style="overflow-y: overlay;">
          <p align="left" v-html="readText"></p>
        </div>
      </div>
      <div>
        <x-button style="width: 100px !important;" @click.native="showDialog=false" mini type="primary">确认</x-button>
        <!-- <x-button mini>取消</x-button> -->
      </div>
    </x-dialog>

    <div class="register_errorMsg" v-show="haveError">
      <badge text="!"></badge> {{errorMessage}}
    </div>
    <x-button text="确认提交" @click.native="processRegister" type="primary" class="register_process_btn"></x-button>
  </div>
</template>
<script>
import { XHeader, XInput, XButton, Badge, XDialog } from "vux";
import { mapState } from "vuex";
import { RouteUrl, Mutations, StrLenth, WE_CHAT } from "src/common/const";
import Validation from "src/common/util/validation";
import { RequestUrl } from "src/common/url";
import WeChatOauth from "src/js/weChat/weChatOauth"; //引入微信授权js
export default {
  components: { XHeader, XInput, XButton, Badge, XDialog },
  computed: mapState(["user", "initUrl", "isWeChar", "saveInsure", "isWeAuth", "shareUuid"]),
  data() {
    return {
      StrLenth: {}, //字符长度常量
      pageTitle: "注册",
      //个人用户注册---------------------------------------------------------------
      employeeCode: "", //员工编码
      nickName: "",
      mobile: "",
      mobileCode: "", //验证码
      imgCode: "", //图形码
      imgCodeSrc: RequestUrl.GET_VALIDATE_CODE, //验证码图片路径
      isSendOk: false, //发送按钮是否禁用
      sendBtnText: "获取验证码", //发送按钮文本内容
      haveError: false, //是否显示错误
      errorMessage: "", //错误信息,
      haveImgCode: false, //是否需要图形码,因为默认为密码登录因此需要,动态码登录第一次不需要

      isCheck: false,//是否已勾选协议
      showDialog: false,//是否显示协议
      readText: "",//协议内容
    };
  },
  methods: {
    goBack() {
      //返回
      this.$common.goBack(this);
    },
    changeImgCode() {
      this.imgCodeSrc = RequestUrl.GET_VALIDATE_CODE + "?a=" + Math.random();
    },
    // 发送验证码
    sendSmsCode() {
      /**
       * 发送验证码,返回data.msg=信息,data.state=0表示成功
       * isSendOk,true=禁用(disabled=true)
       * sendBtnText,验证码按钮文本显示内容
       */
      let imgcode = null;
      if (this.haveImgCode) {
        //需要图形码时传递参数
        imgcode = this.imgCode;
        //校验-图形码
        let msg = Validation.isImgCode(imgcode);
        if (msg != null) {
          this.errorMessage = msg;
          this.haveError = true;
          return;
        }
      }
      this.$common.sendSMS(this, this.mobile, imgcode, this.isSendOk, this.pageTitle);
    },
    //协议勾选
    confirmItem() {
      this.isCheck = !this.isCheck;
      if (this.isCheck) {
        this.errorMessage = "";
        this.haveError = false;
        return;
      }
    },
    //协议展示
    clauseRead(planId, clauseTitle) {
      let query = {
        _this: this,
        TYPE: "FORM",
        planId: planId,
        clauseTitle: clauseTitle
      }
      this.$common.storeCommit(this, Mutations.CUR_CLAUSE_TITLE_ONE, query);
      this.showDialog = true;
    },
    //提交
    processRegister() {
      //已勾选协议
      if (!this.isCheck) {
        this.errorMessage = "请勾选协议"; this.haveError = true; return;
      }
      //默认请求无图形码接口
      let postUrl = RequestUrl.REGISTER;//个人账户注册
      //个人账户注册信息
      let requestDto = {
        TYPE: "FORM",
        employeeCode: this.employeeCode,
        nickName: this.nickName,
        mobile: this.mobile,
        mobileCode: this.mobileCode,
        refereeId: "",
        refereePro: ""
      };
      if (this.saveInsure.refereeData != false) {
        requestDto.refereeId = this.saveInsure.refereeData.userCode;
      }
      if (this.saveInsure.refereePro != false) {
        requestDto.refereePro = this.saveInsure.refereePro;
      }

      //校验-员工编码
      let msg = Validation.isEmployeeCode(requestDto.employeeCode);
      if (msg != null) {
        this.errorMessage = msg; this.haveError = true; return;
      }
      //校验-用户名
      msg = Validation.isNickName(requestDto.nickName);
      if (msg != null) {
        this.errorMessage = msg; this.haveError = true; return;
      }
      //校验-图形码
      if (this.haveImgCode) {
        //个人注册 & 图形码
        postUrl = RequestUrl.REGISTER_IMG;
        requestDto.imgCode = this.imgCode;
        //校验-图形码
        msg = Validation.isImgCode(requestDto.imgCode);
        if (msg != null) {
          this.errorMessage = msg; this.haveError = true; return;
        }
      }
      //校验-手机号
      msg = Validation.isPhone11(requestDto.mobile);
      if (msg != null) {
        this.errorMessage = msg; this.haveError = true; return;
      }
      //校验-手机验证码
      msg = Validation.isSmsCode(requestDto.mobileCode);
      if (msg != null) {
        this.errorMessage = msg; this.haveError = true; return;
      }
      this.haveError = false;

      //普通个人账户注册直接请求
      this.sendRequest(postUrl, requestDto);
    },
    //请求注册
    sendRequest(postUrl, requestDto) {
      let _this = this;
      _this.$common.storeCommit(_this, Mutations.UPDATE_LOADING_STATUS, true);
      _this.$http.post(postUrl, requestDto).then(function (res) {
        _this.$common.storeCommit(_this, Mutations.UPDATE_LOADING_STATUS, false);
        if (res.state != 0) {
          _this.changeImgCode();//更换图形验证码
          _this.errorMessage = res.result;
          _this.haveError = true; //显示错误信息标签
          _this.haveImgCode = true; //启动图形码同时校验
          if (res.result.indexOf("已被") >= 0) {
            _this.isSendOk = false; //启用发送按钮和手机号输入
            _this.sendBtnText = "获取验证码";
            //清除验证码倒计时
            _this.$common.clearTimeOutForSms();
          }
          return;
        }
        //保存USER_INFO信息
        _this.$common.storeCommit(_this, Mutations.SAVE_USER_INFO, res.result);
        _this.$common.goUrl(_this, RouteUrl.MY);
      });
    },
  },
  mounted() {
    document.title = this.pageTitle;
    this.StrLenth = StrLenth; //字符长度常量
    this.$init.subPageInit(this);
    //初始验证码倒计时
    this.$common.clearTimeOutForSms();

    if (this.user.isLogin) {
      this.$common.goUrl(this, RouteUrl.MY);
    } else {
      //微信授权
      WeChatOauth.weChatOauth(this, true, WE_CHAT.OAUTH_TYPE_DEFAULT);
    }
  }
};
</script>
<style lang="scss" scoped>
//input区域
.register_inputDiv {
  margin: 10px auto;
  width: 95%;
  height: auto;
  border-radius: 5px;
  background-color: white;
  .weui-cell {
    border-top: 1px solid #efefef;
  }
  .weui-cell:before {
    border-top: 0px;
  }
}

//图形码
.img__code {
  width: 80px;
  height: 31px;
  margin-left: 3px;
  vertical-align: middle;
}

//换一张图形码
.img__code--change {
  margin-left: 3px;
  vertical-align: middle;
  font-size: 14px;
}

//获取验证码按钮
.linkMobileCode {
  padding: 0px;
  width: 80px;
  height: 28px;
  line-height: 28px;
  font-size: 12px;
  color: #C8161D;
  border: 1px solid #C8161D;
  background-color: white;
}

//注册按钮
.register_process_btn {
  width: 95%;
  font-size: 20px;
}

.register_errorMsg {
  color: red;
  font-size: 14px;
  margin-left: 10px;
  height: 50px;
  line-height: 40px;
}

//修改默认按钮失效时背景颜色
.weui-btn_disabled.weui-btn_primary {
  background-color: #cfcfcf;
}

//修改默认badge位置
.vux-badge-single {
  margin-top: -2px;
}

//协议
.row_protocol {
  margin: 15px auto;
  width: 95%;
  font-size: 14px;
  img {
    width: 14px;
    height: auto;
    float: left;
    margin: 4px 5px 0px 16px;
  }
  span {
    line-height: 18px;
    color: #b1b1b1;
  }
  a {
    color: #C8161D;
    text-decoration: none;
  }
}

.protocol_content {
  border-color: aliceblue;
  white-space: pre-wrap;
  vertical-align: top;
  width: 100%;
  font-size: 15px;
  height: 75vh;
  font-family: Microsoft YaHei;
  overflow-y: scroll; // border: 1px solid #f3f3f3;
  font: 400 14.3333px Arial;
}
.dialog-Einv {
  // padding: 15px !important;
  .weui-dialog {
    max-width: 90% !important;
  }
}
</style>
